package au.csiro.nt.psdp.actions.util;

import au.csiro.nt.psdp.model.User;
import au.csiro.nt.psdp.model.Role;

import java.util.Set;
import java.util.Iterator;

/**
 *
 */
public class RolePermissionsUtil
{
    private RolePermissionsUtil()
    {
    }

    public static boolean isSuperUser(User user)
    {
        return isUserInRole(user, PdspConstants.ROLE_SUPER_USER);
    }

    public static boolean isOrganisation(User user)
    {
        return isUserInRole(user, PdspConstants.ROLE_ORGANISATION);
    }

    public static boolean isUser(User user)
    {
        return isUserInRole(user, PdspConstants.ROLE_USER);
    }

    public static boolean isUserInRole(User user, String roleName)
    {
        if (user == null || roleName == null)
        {
            return false;
        }

        Set<Role> roles = user.getRoles();
        for (Iterator<Role> rolesIt = roles.iterator(); rolesIt.hasNext();)
        {
            Role role = rolesIt.next();
            if (roleName.equals(role.getName()))
            {
                return true;
            }
        }
        return false;
    }

}
